;(function(){

	var defaults = {
		class:'imglazyload',
	}

	function imglazyload(domFather,opt){
		this.init(domFather,opt);
	}

	imglazyload.prototype = {

		init:function(domFather,opt){
			this.opt = opt;
			var tthis = this;
			var domImg = domFather.find('.imglazyload');

			$.each(domImg,function(i){
				tthis.loading(domImg.eq(i));
			});
		},
		loading:function(imgDom){
			var tthis = this,newImg;
			var imgSrc = imgDom.attr('src'),
				imgUrl = imgDom.attr('url'),
				imgBool = imgDom.attr('bool');

			if(!imgBool){
				imgDom.attr({'src':imgUrl,'url':imgSrc,'bool':'true'});
			}
			else{
				
				return;
			}

			newImg = new Image();
			newImg.src = imgSrc;
			newImg.onload = function(){
				tthis.center(imgDom,this);
				tthis.setSrc(imgDom);
			}

		},
		setSrc:function(imgDom){
			var imgSrc = imgDom.attr('src'),
				imgUrl = imgDom.attr('url');
			imgDom.attr({'src':imgUrl,'url':imgSrc});
			imgDom.css({'opacity':0}).animate({'opacity':1});
		},
		center:function(imgDom,newImg){
			var rate,
				newImgW = newImg.width,
				newImgH = newImg.height,
				fatherDomW = imgDom.parent().width(),
				fatherDomH = imgDom.parent().height();

				// console.log(newImgW,newImgH)
				// console.log(fatherDomW,fatherDomH)
			if(newImgW <= newImgH){
				rate = fatherDomW / newImgW;
				imgDom.css({
					'width': fatherDomW + 'px',
					'height': rate * newImgH + 'px',
					'marginTop': - (rate * newImgH - fatherDomH)/2 + 'px',
				});
			}else{
				rate = fatherDomH / newImgH;
				imgDom.css({
					'height': fatherDomH + 'px',
					'width': rate * newImgW + 'px',
					'marginLeft': - (rate * newImgW - fatherDomW)/2 + 'px',
				});
			}
		}
	}

	$.fn.imglazyload = function(opt){
		var domFather = this,
			opt = $.extend({},defaults,opt);
		var newFun = new imglazyload(domFather,opt);
	}
})();